# -*- coding: utf-8 -*-
import sip
sip.setapi('QString', 2)

from PyQt4 import QtGui, QtCore, uic
import sys, functools, os
import parser
import analytics as analytics
import overmind as overmind
reload(overmind)
reload(parser)
reload(analytics)

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
form_class, base_class = uic.loadUiType(os.path.join(SCRIPT_DIR, "gui.ui"))

class TeamToolGui(base_class, form_class):
	def __init__(self):
		super(base_class, self).__init__()
		self.setupUi(self)
		self.sum_name_inputs = [self.sum_name_1, self.sum_name_2, self.sum_name_3, self.sum_name_4, self.sum_name_5, self.sum_name_6]
		self.sum_id_inputs = [self.sum_id_1, self.sum_id_2, self.sum_id_3, self.sum_id_4, self.sum_id_5, self.sum_id_6]
		self.champ_inputs = [self.champ_1, self.champ_2, self.champ_3, self.champ_4, self.champ_5]
		self.e_champ_inputs = [self.e_champ_1, self.e_champ_2, self.e_champ_3, self.e_champ_4, self.e_champ_5]
		self.completer = QtGui.QCompleter(overmind.CHAMPS, self)
		for i in range(6):
			self.sum_name_inputs[i].returnPressed.connect(functools.partial(self.get_id, i))
			if i < 5:
				self.champ_inputs[i].setCompleter(self.completer)
				self.e_champ_inputs[i].setCompleter(self.completer)

		self.analyze_btn.clicked.connect(self.analyze_btn_clicked)
		self.counters_btn.clicked.connect(self.get_counters)

		self.test_data_btn.clicked.connect(self.test_data_clicked)

	def test_data_clicked(self):
		data = (("28790666", "ezreal"), ("30266087", "diana"),("19848599", "olaf"),("25510057", "soraka"),("25430055", "caitlyn"),)
		for i in range(5):
			self.sum_id_inputs[i].setText(data[i][0])
			self.champ_inputs[i].setText(data[i][1])

	def get_counters(self):
		a_champs = []
		e_champs = []
		print "\n"
		print "@@@@@@@@@@@@@@@@@@@@@@@@@@"
		for i in range(5):
			e_champ = self.e_champ_inputs[i].text().strip()
			counters = None
			a_champ = self.champ_inputs[i].text().strip()
			if a_champ:
				a_champs.append(a_champ)
			if e_champ:
				e_champs.append(e_champ)
				counters = analytics.get_counter(e_champ)
				if counters:
					_tmp = "\n"
					for c in counters: 
						_tmp += "%s, "%c
					print "%s IS COUNTERED BY: %s"%(e_champ, ", ".join(counters))
		ascore, escore, amsg, emsg = analytics.get_counter_score(a_champs, e_champs)
		print "\n"
		print ascore, escore
		print amsg
		print emsg
		print "@@@@@@@@@@@@@@@@@@@@@@@@@@\n"

	def get_id(self, *args):
			i = args[0]
			name = self.sum_name_inputs[i].text().replace(" ", "+")
			id = parser.get_id_from_name(name, False)
			self.sum_id_inputs[i].setText(id)

	def analyze_btn_clicked(self):
		team = []
		e_champs = []
		a_champs = []
		msg = ""
		id_name_map = {}
		for i in range(5):
			_p = {}
			name = self.sum_name_inputs[i].text().strip()
			id = self.sum_id_inputs[i].text().strip()
			champ = self.champ_inputs[i].text().strip()
			e_champ = self.e_champ_inputs[i].text().strip()
			if e_champ:
				e_champs.append(e_champ)
			if not id: continue
			if not name: name = id
			id_name_map[id] = name
			s = analytics.Summoner(id)
			if not champ: champ = None
			else: a_champs.append(champ)

			_tmp1, _tmp2 = "", "%s plays best as: "%name
			for bc in s.get_best_champs()[:5]:
				_tmp1 += "%s: %.2f - %s, "%(bc[1], bc[0], int(bc[2]))
				_tmp2 += "%s, "%bc[1]
			msg += "%s\n%s\n"%(_tmp2, _tmp1)
			rankings = s.get_overall_tag_rankings()

			for tag in ["carry", "support", "assassin", "fighter"]:
				msg+= "%s: %.2f, \t"%(tag, rankings.get(tag, -.5))
			msg += "\n\n"


			_p["id"], _p["summoner"], _p["champ"] = id, s, champ
			team.append(_p)
		print "#######################################"
		print msg
		res = analytics.analyze_team(team)
		for id, ranking in res[2].items():
			print "%s: %.2f %s as %s - %s"%(id_name_map[id], ranking[0], ranking[1], ranking[2], str(int(ranking[3])))
		print "#totals#"
		print "carry ranking: %.2f,  support ranking: %.2f"%(res[0], res[1])
		print "#######################################"
		print "\n\n"
		#print players, e_champs

def main():
	app = QtGui.QApplication(sys.argv)
	global lolgui
	try: lolgui.close()
	except: pass
	lolgui = TeamToolGui()
	lolgui.show()
	sys.exit(app.exec_())

main()

